//
//  LoginVC.swift
//  QQZone
//
//  Created by xmg on 16/9/1.
//  Copyright © 2016年 wlis. All rights reserved.
//

import UIKit

class LoginVC: UIViewController {

    /** 账号 */
    @IBOutlet weak var AccountNumber: UITextField!
    /** 密码 */
    @IBOutlet weak var Password: UITextField!
    /** 登陆 */
    @IBOutlet weak var Land: UIButton!
    /** 记住密码 */
    @IBOutlet weak var RememberPwd: UIButton!
    /** 自动登陆 */
    @IBOutlet weak var AutoLogin: UIButton!
    /** 登陆的View控件 */
    @IBOutlet weak var LoginView: UIView!
    
    override func viewDidLoad() {
        super.viewDidLoad()
 
        /** 解决报错问题 */
        view.translatesAutoresizingMaskIntoConstraints = false
      
    }
    override func didReceiveMemoryWarning() {
        super.didReceiveMemoryWarning()
        // Dispose of any resources that can be recreated.
    }
    override func touchesBegan(touches: Set<UITouch>, withEvent event: UIEvent?) {
        self.view.endEditing(true);
    }

}

extension LoginVC{

    /** 登陆 */
    @IBAction func LandBtnClick(sender: UIButton) {
        
        let account = AccountNumber.text!
        let pwd = Password.text!
        if  account.characters.count == 0 {
            showErrorInfo("账号不能为空,请输入!")
            return
        }
        if pwd.characters.count == 0 {
            showErrorInfo("密码不能为空,请输入!")
            return
        }
        
        if account == "admin" && pwd == "admin" {
        let mainVc = HomeVc()
            presentViewController(mainVc, animated: false, completion: nil)
        }else{
        showErrorInfo("账号或者密码错误")
        }
    }
    /** 记住密码 */
    @IBAction func RememberPassword(sender: UIButton) {
    RememberPwd.selected = !RememberPwd.selected
        if RememberPwd.selected{
        AutoLogin.selected = false
        }
    }
    /** 自动登陆 */
    @IBAction func AutoLogin(sender: UIButton) {
    AutoLogin.selected = !AutoLogin.selected
        if AutoLogin.selected{
        RememberPwd.selected = true
        }
    }
}

extension LoginVC{

    private func showErrorInfo(errorInfo : String){
        /** 创建提示 */
        let alertVc = UIAlertController(title: "登陆失败", message: errorInfo, preferredStyle: .Alert)
        let action = UIAlertAction(title: "确定", style: .Cancel, handler: nil )
        alertVc.addAction(action)
        /** 莫大出控制器 */
        presentViewController(alertVc, animated: true, completion: nil)
        
        /** 设置抖动 */
        let shakeAnim = CAKeyframeAnimation(keyPath: "transform.translation.x")
        shakeAnim.values = [-10,0,10,0]
        shakeAnim.repeatCount = 3
        shakeAnim.duration = 0.15
        LoginView.layer.addAnimation(shakeAnim, forKey: nil)
        
        
    }
}
